// pages/releaseAvatar/releaseAvatar.js
import { uploadFile } from '../../utils/uploadFile.js';
const config = require('../../utils/config.js');

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityCoverPage:'',
    userName:'',
    avatar:'',
    butB:app.globalData.minModel ? '60px':'89px', //按钮底部距离
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    console.log(options.tempfilepaths)
    if(options && options.tempfilepaths){
      this.setData({
        avatar:options.tempfilepaths,
        userName:options.userName
      })
    }
    this.setData({
      activityCoverPage:wx.getStorageSync('lookCoverPage'),
    })

  },
  chooseImage(){
    let than = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success (res) {
        console.log(res)
        // tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths)

        // let params = {
        //   userId:wx.getStorageSync('userId'),
        //   fileUpload:tempFilePaths[0],
        //   type:1
        // }
        // uploadFile('user/uploadAvtar','1',params).then(res=>{
        //   console.log('文件上传成功回调')
        //   console.log(this)
        //   than.setData({
        //     avatar:tempFilePaths[0]
        //   })
        // })

        //去裁剪
        wx.redirectTo({
          url: '/pages/trimImage/trimImage?tempFilePaths='+tempFilePaths[0]+'&userName='+ than.data.userName
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  bindKeyInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  getdata(){
    const { avatar, userName } = this.data;
    if(!userName){
      wx.showToast({
        title: '请填写昵称',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if(!avatar){
      wx.showToast({
        title: '请上传头像',
        icon: 'none',
        duration: 1000
      })
      return false;
    }

    let header = {
      'content-type': 'application/json',  //json格式
    }
    wx.request({
      url: config.host + '/user/updateUserInfo',
      method: "POST",
      data: {'userName':userName,'userId':wx.getStorageSync('userId')},
      header: header,
      success: function(res) {
        if(res.data.success){
          app.globalData.userInfo.status = 1 //0为需要登
          wx.setStorageSync('status', 1)

          wx.navigateBack({
            delta: 2
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function(error) {
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})